/*
 * Loader manager.
 */

/*
 * default library path
 */
var libPath = "workspace/Libraries/";

/*
 * loaddcmd & loader tool.
 */
var loaddcmd = "";
var loader = "propeller-load";
//var loader = "p1load";

var isWindows= /^win/.test(process.platform);

/*
 * required modules
 */
var fs    = require('fs');
var exec  = require('child_process').exec;
var execf = require('child_process').execFile;

/*
 * loader arguments
 */
var options  = "";

module.exports = {
  loadRAM:
  function(file, callback) {
    loadRAM(file, callback);
  },
  loadEEPROM:
  function(file, callback) {
    loadEEPROM(file, callback);
  }
};

/*
 * load program to RAM
 */
function loadRAM (file, callback)
{
  if (fs.existsSync(file)) {
    var pwd= process.cwd();
    //loadcmd = "nice -20 ";
    loadcmd = loader;
    loadcmd += ' -r "'+pwd+'/'+file+'"';
    if(isWindows) {
      while (loadcmd.indexOf('/') > -1) {
        loadcmd = loadcmd.replace('/','\\');
      }
    }
    console.log("LoadRAM: "+loadcmd);
    exec(loadcmd, callback);
  }
}

/*
 * load program to EEPROM
 */
function loadEEPROM (file, callback)
{
  if (fs.existsSync(file)) {
    var pwd= process.cwd();
    loadcmd = loader;
    loadcmd += ' -r -e "'+pwd+'/'+file+'"';
    if(isWindows) {
      while (loadcmd.indexOf('/') > -1) {
        loadcmd = loadcmd.replace('/','\\');
      }
    }
    console.log("LoadEEPROM: "+loadcmd);
    exec(loadcmd, callback);
  }
}


